#!/usr/bin/env bash

#support multiple column print out
#args ... , each arg as one column
show_cmd_info()
{
    local col1="cmd:";
    local col2="$1"
    unset cols
    unset len_col

    while [ -n "$1" ]
    do
        #echo --------$1
        cols[${#cols[@]}]="$1"
        shift
    done

    for((i=0;i<${#cols[@]};++i))
    do
        len_col[i]=${#cols[i]}
    done


    local len_total=70
    #echo ${len_col[@]}

    local len_sum=0
    for len in ${len_col[@]}
    do
        let len_sum=len_sum+len+1
    done

    #echo $len_sum
    let len_col[${#len_col[@]}]=len_total-len_sum-1  #1:len of +

    #echo ${len_col[@]}
    #printf "+%s+%s+%s\n" "$(eval printf %.s- {1..${len_col[0]}})" "$(eval printf %.s- {1..${len_col[1]}})" "$(eval printf %.s- {1..${len_col[2]}})"

    local coli=0
    for len in ${len_col[@]}
    do
        printf "+%s" "$(eval printf %.s- {1..${len_col[$coli]}})"
        let coli++

        #the last column need appen '|'
        if [ $[coli] -gt ${#cols[@]} ] ; then
            printf "\n"
            break;
        fi
    done

    #echo "all count:${#cols[@]}"
    #print row of data string
    for((i=0;i<${#cols[@]};++i))
    do
        printf "|%s" "${cols[i]}"
        #the last column need appen '|'
        if [ $[i+1] -ge ${#cols[@]} ] ; then
            printf "|\n"
            break;
        fi
    done


    coli=0
    for len in ${len_col[@]}
    do
        printf "+%s" "$(eval printf %.s- {1..${len_col[$coli]}})"
        let coli++

        #the last column need appen '+'
        if [ $[coli+1] -ge ${#len_col[@]} ] ; then
            printf "+\n"
            break;
        fi
    done
}
#for test
#show_cmd_info "cat /etc/redhat-release" "new me"

print_brand()
{
    printf "\n"
    unset brands
    while [ -n "$1" ] 
    do
        brands[${#brands[@]}]="$1"
        shift
    done

    local total=66
    local cmd_str=""
    cmd_str='printf -v line_str '%.s=' {1..'$total'}'
    #echo $cmd_str
    eval "$cmd_str"
    echo $line_str

    for((i=0; i<${#brands[@]}; ++i))
    do
        local brand_str=${brands[i]}
        len_brand=${#brand_str}
        #echo $len_brand
        ((len=$len_brand+($total>>1)-($len_brand>>1)))
        cmd_str='printf "%'${len}'s\n" "'$brand_str'"'
        #echo "===========:$cmd_str"
        eval "$cmd_str"
    done
    echo $line_str
}

execute_cmd()
{
    show_cmd_info cmd: "$1"
    eval "$1"
}

main()
{
    execute_cmd "cat /etc/redhat-release"
    execute_cmd "cat /proc/version"
    execute_cmd "cat /etc/redhat-release"
    execute_cmd "lsb_release -a"
    execute_cmd "uname -a"
    execute_cmd "cat /etc/issue"

    print_brand "powered by igcc.cc" "date: $(date '+%Y-%m-%d %H:%M:%S')" "email: vimcpp@163.com"
    return 0
}

main && exit 0 || exit 1


